From ed55c696cd083bfaa5429082a9c5edd4ebde0cd8 Mon Sep 17 00:00:00 2001
From: Sebastien Castiel <sebastien@castiel.me>
Date: Tue, 5 Dec 2023 17:39:05 -0500
Subject: First version

---
 .../[groupId]/expenses/[expenseId]/edit/page.tsx   | 42 ++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 src/app/groups/[groupId]/expenses/[expenseId]/edit/page.tsx

(limited to 'src/app/groups/[groupId]/expenses/[expenseId]/edit/page.tsx')

diff --git a/src/app/groups/[groupId]/expenses/[expenseId]/edit/page.tsx b/src/app/groups/[groupId]/expenses/[expenseId]/edit/page.tsx
new file mode 100644
index 0000000..f22ac08
--- /dev/null
+++ b/src/app/groups/[groupId]/expenses/[expenseId]/edit/page.tsx
@@ -0,0 +1,42 @@
+import { ExpenseForm } from '@/components/expense-form'
+import { Button } from '@/components/ui/button'
+import { getExpense, getGroup, updateExpense } from '@/lib/api'
+import { expenseFormSchema } from '@/lib/schemas'
+import { ChevronLeft } from 'lucide-react'
+import Link from 'next/link'
+import { notFound, redirect } from 'next/navigation'
+
+export default async function EditExpensePage({
+  params: { groupId, expenseId },
+}: {
+  params: { groupId: string; expenseId: string }
+}) {
+  const group = await getGroup(groupId)
+  if (!group) notFound()
+  const expense = await getExpense(groupId, expenseId)
+  if (!expense) notFound()
+
+  async function updateExpenseAction(values: unknown) {
+    'use server'
+    const expenseFormValues = expenseFormSchema.parse(values)
+    await updateExpense(groupId, expenseId, expenseFormValues)
+    redirect(`/groups/${groupId}`)
+  }
+
+  return (
+    <main>
+      <div className="mb-4">
+        <Button variant="ghost" asChild>
+          <Link href={`/groups/${groupId}`}>
+            <ChevronLeft className="w-4 h-4 mr-2" /> Back to group
+          </Link>
+        </Button>
+      </div>
+      <ExpenseForm
+        group={group}
+        expense={expense}
+        onSubmit={updateExpenseAction}
+      />
+    </main>
+  )
+}
-- 
cgit v1.3

